﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Business;
using System.Collections.Generic;
using Entities;
using System.Globalization;

namespace eProjectRailWayReservation.Admin
{
    public partial class UpdateTicket1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session.Add("page", Define.EMP_STATION);
            if (!Page.IsPostBack)
            {
                GetData();
            }
        }

        public void GetData()
        {
            if (TicketBUS.GetList_TicketInfo().Rows.Count > 0)
            {
                gvTicket.DataSource = TicketBUS.GetList_TicketInfo();
                gvTicket.DataBind();
                gvTicket.UseAccessibleHeader = true;
                if (gvTicket.HeaderRow != null)
                {
                    gvTicket.HeaderRow.TableSection = TableRowSection.TableHeader;
                }
            }
            else
            {
                ShowNoResultFound(TicketBUS.GetList_TicketInfo(), gvTicket);
            }
        }

        private void ShowNoResultFound(DataTable source, GridView gv)
        {
            source.Clear();
            source.Rows.Add(source.NewRow()); // create a new blank row to the DataTable
            // Bind the DataTable which contain a blank row to the GridView
            gv.DataSource = source;
            gv.DataBind();
            // Get the total number of columns in the GridView to know what the Column Span should be
            int columnsCount = gv.Columns.Count;
            gv.Rows[0].Cells.Clear();// clear all the cells in the row
            gv.Rows[0].Cells.Add(new TableCell()); //add a new blank cell
            gv.Rows[0].Cells[0].ColumnSpan = columnsCount; //set the column span to the new added cell

            //You can set the styles here
            gv.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
            //gv.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Red;
            //gv.Rows[0].Cells[0].Font.Bold = true;
            //set No Results found to the new added cell

            gv.Rows[0].Cells[0].Text = "No results!";
            //gv.CssClass = "list";
            gv.HeaderRow.TableSection = TableRowSection.TableHeader;
        }

        protected void lblPay_Click(object sender, EventArgs e)
        {
            Ticket temp = new Ticket();
            foreach (GridViewRow row in gvTicket.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("cbSelectAll");
                if (cb.Checked)
                {
                    int id = int.Parse(row.Cells[1].Text);
                    string status = row.Cells[9].Text;

                    if (status == "Pending")
                    {
                        temp.TicketID = id;
                        temp.Status = 1;
                        bool k = TicketBUS.Update_Ticket_Status(temp);
                    }
                    else
                    {
                        
                    }
                }
            }
            GetData();      
        }

        protected void lblCancel_Click(object sender, EventArgs e)
        {
            Ticket temp = new Ticket();
            foreach (GridViewRow row in gvTicket.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("cbSelectAll");
                if (cb.Checked)
                {
                    int id = int.Parse(row.Cells[1].Text);
                    string status = row.Cells[9].Text;
                    if (status == "Pending")
                    {
                        bool k = TicketBUS.Delete_Ticket(id);
                    }
                    else
                    {
                        if (status == "Complete")
                        {
                            temp.TicketID = id;
                            temp.Status = 2;
                            bool h = TicketBUS.Update_Ticket_Status(temp);
                        }    
                    }
                }
            }
            GetData();
        }

        protected void gvTicket_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTicket.PageIndex = e.NewPageIndex;
            gvTicket.DataSource = TicketBUS.GetList_TicketInfo();
            gvTicket.DataBind();
        }

        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (TicketBUS.Search_TicketByStatus(Convert.ToInt32(ddlStatus.SelectedValue)).Rows.Count > 0)
            {
                gvTicket.DataSource = TicketBUS.Search_TicketByStatus(Convert.ToInt32(ddlStatus.SelectedValue));
                gvTicket.DataBind();
                gvTicket.UseAccessibleHeader = true;
                if (gvTicket.HeaderRow != null)
                {
                    gvTicket.HeaderRow.TableSection = TableRowSection.TableHeader;
                }
            }
            else
            {
                ShowNoResultFound(TicketBUS.Search_TicketByStatus(Convert.ToInt32(ddlStatus.SelectedValue)), gvTicket);
            }
        }

        protected void lblSearch_Click1(object sender, EventArgs e)
        {
            if (txtCreateDate.Text == "")
            {
                ShowMessage("Please Input Date");
                txtCreateDate.Focus();
            }
            else
            {
                DateTime d = ConvertToDateTimeSQL(txtCreateDate.Text);
                int status = Convert.ToInt32(ddlStatus.SelectedValue);
                if (txtCustomerName.Text == null)
                {
                    txtCustomerName.Text = "";
                }
                if (TicketBUS.Search_TicketInfo(txtCustomerName.Text, d, status).Rows.Count > 0)
                {
                    gvTicket.DataSource = TicketBUS.Search_TicketInfo(txtCustomerName.Text, d, status);
                    gvTicket.DataBind();
                    gvTicket.UseAccessibleHeader = true;
                    if (gvTicket.HeaderRow != null)
                    {
                        gvTicket.HeaderRow.TableSection = TableRowSection.TableHeader;
                    }
                }
                else
                {
                    ShowNoResultFound(TicketBUS.Search_TicketByStatus(Convert.ToInt32(ddlStatus.SelectedValue)), gvTicket);
                }
            }
        }

        public DateTime ConvertToDateTimeSQL(string Content)
        {
            //Store Original DateTime Type from User Input
            DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
            dtfi.ShortDatePattern = "MM/dd/yyyy";
            dtfi.DateSeparator = "/";
            DateTime d = Convert.ToDateTime(Content, dtfi);
            // Convert datetime type to SQL datetime
            DateTimeFormatInfo dtfi2 = new DateTimeFormatInfo();
            dtfi.ShortDatePattern = "dd/mm/yyyy";
            dtfi.DateSeparator = "/";
            DateTime d2 = new DateTime();
            d2 = Convert.ToDateTime(d, dtfi2);         
            return d2;
        }

        public void ShowMessage(string Content)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('" + Content + "');</script>");
        }
    }
}
